<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container>
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="表名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tableShowName">
              <a-input v-model="model.tableShowName" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="联合keys" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="keyCodes">
              <a-input v-model="model.keyCodes" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="分区键" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="partitionKey">
              <a-select    v-model="model.partitionKey" >
                <a-select-option v-for="item in partitionKeys" :key="item.id" :value="item.logicCode">
                  {{ item.logicName }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item  v-if="shouldShowFormItem"  label="时间分区策略" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="partitionKey">
              <a-select  v-model="model.partitionDataType" >
                <a-select-option value="month">按月分区</a-select-option>
                <a-select-option value="day">按日分区</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-item :wrapperCol="{span: 19, offset: 5}">
              <a-button style="margin-left: 8px" @click="prevStep">上一步</a-button>
              <a-button type="primary" @click="nextStep">下一步</a-button>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { getExtInfoById,getDAListByReq,updateExtInfo } from '@api/apiDatalib'

  export default {
    data () {
      return {
        description: '逻辑表设置',
        confirmLoading: false,
        model:{},
        tableId:'',
        partitionKeys:[],
        dicKeys:[],

        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        dbSourceOptions:[],
        validatorRules: {
          disKey: [{ required: true, message: '请输入分桶键'}],
          disNum: [{ required: true, message: '请输入分桶数'}],
        },
        url: {
          addTable:"/dcloud-platform/api/datalibLogicTable/v1/add",
          editTbale:"/dcloud-platform/api/datalibLogicTable/v1/edit"
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      addData: {
        type: Object, // 根据实际数据类型定义
        required: true, // 如果这个prop是必需的，可以设置为true
      },
    },
    name: "Step1",
    mounted() {
       this.loadFieldAct()
    },
    watch:{
      addData:{
        deep: true,
        immediate: true,
        handler(val){
          console.log('this.addData',this.addData)
          this.tableId =this.addData.id
          this.loadSource()
        }
      }},
    computed: {
      shouldShowFormItem(){
        console.log(this.model)
        if(this.model.partitionKey){
          let find = this.partitionKeys.find(e => e.logicCode == this.model.partitionKey)
          return find.logicDataType === 'DATE' || find.logicDataType === 'DATETIME'
        }

        return false
      }
    },
    methods: {


      loadSource(){
        //根据tableId查询表的扩展设置信息
        getExtInfoById({tableId:this.tableId}).then((res) => {
           if(res.success){
             this.model = res.result
           }else{
             this.$message.error(res.message);
           }
        }).catch(
          () => {
            this.$message.error('获取表信息失败!');
          }
        )
      },

      loadFieldAct(){
        let param={"tableId":this.addData.id}
        param['selectType'] = 'keyType'
        getDAListByReq(param).then((res) => {
          if(res.success){
            this.partitionKeys = res.result;
          }
        })
        let param2={"tableId":this.addData.id}
        param2['selectType'] = 'noDateType'
        getDAListByReq(param2).then((res) => {
          if(res.success){
            this.dicKeys = res.result;
          }
        })
      },
      nextStep () {
        let that = this
        this.$refs.form.validate(valid => {
          if (valid) {
            //修改表扩展信息
            updateExtInfo(this.model).then((res) => {
              if(res.success){

                that.$emit('nextStep')
              }else{
                this.$message.error(res.message);
              }
            })
          }
        })
      },
      prevStep(){
        this.$emit('prevStep')
      }

    },


  }
</script>

<style scoped>

</style>